#include "PatientRecord.h"

#include <QDebug>

PatientRecord::PatientRecord() :
    ASTM()
{

}

QString PatientRecord::buildRecord() const
{
    QString record = recordTypeSymbol(RecordType::Patient);
    record += FIELD;
    record += QString::number(frameIndex);
    record += FIELD;
    record += id;
    record += FIELD;
    record += name;
    record += FIELD;
    record += age;
    record += FIELD;
    record += sexTypeList.value(sexType);
    record += FIELD;
    record += visitTypeList.value(visitType);
    record += FIELD;
    record += bed;
    record += FIELD;
    record += doctor;
    record += FIELD;
    record += department;
    record += CR;

    for(auto &order: orders) {
        record += order.buildRecord();
    }

    return record;
}

bool PatientRecord::analyzeRecord(const QString &str)
{
    QStringList fields = str.split(FIELD);

    if(fields.count() < 9 || fields.value(0) != recordTypeSymbol(RecordType::Patient)) {
        // qWarning() << "PatientRecord: Syntax Error!";
        return false;
    }

    id = fields[2];
    name = fields[3];
    age = fields[4];
    if(sexTypeList.indexOf(fields[5]) >= 0)
        sexType = static_cast<SexType>(sexTypeList.indexOf(fields[5]));

    if(visitTypeList.indexOf(fields[6]) >= 0)
        visitType = static_cast<VisitType>(visitTypeList.indexOf(fields[6]));

    bed = fields[7];
    doctor = fields[8];
    department = fields[9];

    return true;
}

void PatientRecord::addOrder(const OrderRecord &order)
{
    int orderFrameIndex = orders.count() + 1;

    orders << order;
    orders.last().frameIndex = orderFrameIndex;
}

int PatientRecord::orderIndexOf(const QString sampleId) const
{
    int index = -1;

    for(int i = 0; i < orders.count(); i++) {
        if(sampleId == orders[i].sampleID) {
            index = i;
            break;
        }
    }

    return index;
}
